<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Interfaces List</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
        }
        h1 {
            color: #333;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f4f4f4;
            font-weight: bold;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #f1f1f1;
        }
        .endpoint {
            font-weight: bold;
            color: #007BFF;
        }
        .methods {
            color: #28a745;
        }
        .url {
            color: #6c757d;
        }
    </style>
    <script>
        function callEndpoint(endpoint, methods) {
            fetch(`/${endpoint}`, {method:methods})
                .then(response => response.json())
                .then(data => alert(JSON.stringify(data)))
                .catch(error => alert('Error calling endpoint: ' + error));
        }

        function openAdd() {
            const addRow = document.getElementById('addRow');
            if (addRow.style.display === 'none') {
                addRow.style.display = '';
            } else {
                addRow.style.display = 'none';
            }
        }

        function addInterface() {
            const endpoint = document.querySelector('.i_endpoint').value;
            const method = document.querySelector('.i_methods').value;
            const response = document.querySelector('.i_response').value;

            if (!endpoint || !method || !response) {
                alert('Please fill in all fields');
                return;
            }

            fetch('/add_endpoint', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    method: method,
                    path: endpoint,
                    response: JSON.parse(response)
                })
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                window.location.reload(); // 刷新页面以显示新添加的接口
            })
            .catch(error => alert('Error adding endpoint: ' + error));
        }

        function deleteInterface(endpoint, method) {
            if (confirm('Are you sure you want to delete this endpoint?')) {
                fetch('/delete_endpoint', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        method: method,
                        path: endpoint
                    })
                })
                .then(response => response.json())
                .then(data => {
                    alert(data.message);
                    window.location.reload(); // 刷新页面以显示删除后的接口列表
                })
                .catch(error => alert('Error deleting endpoint: ' + error));
            }
        }
    </script>
</head>
<body>
    <h1>Custom Interfaces List</h1>
    <table>
        <thead>
            <tr>
                <th>Endpoint</th>
                <th>Methods</th>
                <th>Response</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            {% for interface in interfaces %}
            <tr>
                <td class="endpoint" onclick="callEndpoint('{{ interface.endpoint}}','{{interface.methods[0]}}')">{{ interface.endpoint }}</td>
                <td class="methods">{{ interface.methods | join(', ') }}</td>
                <td class="response">{{ interface.response | tojson | safe }}</td>
                <td class="endpoint" onclick="deleteInterface('{{ interface.endpoint }}', '{{ interface.methods[0] }}')">删除</td>
            </tr>
            {% endfor %}
            <tr id="addRow" style="display: none;">
                <td><input type="text" class="i_endpoint"></td>
                <td><select class="i_methods">
                    <option value="GET">GET</option>
                    <option value="POST">POST</option>
                    <option value="PUT">PUT</option>
                    <option value="DELETE">DELETE</option>
                </select></td>
                <td><input type="text" class="i_response"></td>
                <td class="endpoint" onclick="addInterface()">保存</td>
            </tr>
            <tr>
                <td><input type="button" onclick="openAdd()" title="➕" value="➕"></td>
            </tr>
        </tbody>
    </table>
</body>
</html>